package com.bcd.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbManager{	
	private static DbManager _instance;
	private Connection _conn = null;
	private Statement _stmt = null;
	private ResultSet _rs = null;
	
	private DbManager(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");			
		} catch (ClassNotFoundException e) {
			System.out.println("DB Driver Class not found!");
		}
		
	}

	public static DbManager getInstance(){
		if(_instance == null) {
			_instance = new DbManager();
		}
		return _instance;
	}
	
	private void setConnection(){
		try {
			_conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bcdauctions","root","9114");
		} catch (SQLException e) {
			System.out.println("Could not get DB Connection! " + e.getMessage());
		}
	}
	
	public ResultSet ExecuteSelect(String statement){
		setConnection();
		try {
			_stmt = _conn.createStatement();
			_rs = _stmt.executeQuery(statement);			
		} catch (SQLException e) {
			System.out.println("Could not execute Select: " + e.getMessage());
		}
		return _rs;
	}
	
	public Integer ExecuteUpdate(String statement){
		setConnection();
		Integer num_rows = 0;
		try {		
			_stmt = _conn.createStatement();
			num_rows = _stmt.executeUpdate(statement);			
		}catch(SQLException e){
			System.out.println("Could not execute Update: " + e.getMessage());
		}
		return num_rows;
	}
	
	
}